import { useUserStore, useRouterStore } from '@/store'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'

/*
  单点登录
*/
export const useYKBSSOLogin = () => {
  // `location.search` 得到url中?之后#之前的部分
  // 获取当前页面的 URL
  const url = new URL(window.location.href)
  // // 使用 URLSearchParams 提取查询参数
  // const params = new URLSearchParams(url.search)
  // console.log('params', params)
  // // 获取 auth_code 的值
  // const authCode = params.get('authCode')

  let paramsStr = url.href.split('?')
  const params = new URLSearchParams(paramsStr[1])
  const authCode = params.get('authCode')

  // 输出 auth_code 的值

  const userStore = useUserStore()
  const routerStore = useRouterStore()

  const router = useRouter()

  if (authCode === null || authCode === undefined) {
    //window.location.href = 'http://10.11.169.153:8102/'
    return
  }

  routerStore.setState({
    loading: true,
    closeLoading: false
  })

  userStore
    .ybkSsoLogin({
      authCode
    })
    .then((res) => {
      console.log(11111)
      console.log(res)
      if (res) {
        console.log(22222)
      } 
    }).catch(() => {
      //未纳管
      ElMessageBox.alert('账号未纳管，无法进入', '提示', {})
      // router.push({
      //   name: 'Error401'
      //  })
      window.location.href = 'https://mzj.zgdxyyfgs.com:8000/system/#/error401'
    }).finally(() => {
      setTimeout(() => {
          routerStore.setState({
            loading: false,
            closeLoading: true
          })
        }, 2000)
    })
}